package org.csu.mypetstorems.controller;

import jakarta.validation.Valid;
import org.csu.mypetstorems.entity.Category;
import org.csu.mypetstorems.entity.Item;
import org.csu.mypetstorems.entity.PageBean;
import org.csu.mypetstorems.entity.Product;
import org.csu.mypetstorems.result.Result;
import org.csu.mypetstorems.service.CatalogService;
import org.csu.mypetstorems.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CatalogController {
    @Autowired
    private CatalogService catalogService;

    @PostMapping("/item")
    public Result<String> itemAddService(@Valid @RequestBody Item item, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.error(bindingResult.getFieldError().getDefaultMessage());
        }
        if (catalogService.addItem(item)) {
            return Result.success();
        }else {
            return Result.error("itemid重复");
        }
    }

    @PutMapping("/item")
    public Result<String> itemEditService(@Valid @RequestBody Item item, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.error(bindingResult.getFieldError().getDefaultMessage());
        }
        if (catalogService.updateItem(item)) {
            return Result.success();
        }else {
            return Result.error("更新item失败");
        }
    }

    @PatchMapping("/item")
    public Result<String> itemChangeStateService(@RequestParam String itemId,@RequestParam String status) {
        if (catalogService.updateItemStatus(status, itemId)) {
            return Result.success();
        }else {
            return Result.error("更新item状态失败");
        }
    }

    @DeleteMapping("/item")
    public Result<String> itemDeleteService(@RequestParam String itemId) {
        if (catalogService.deleteItem(itemId)) {
            return Result.success();
        }else {
            return Result.error("删除item失败");
        }
    }

    @GetMapping("/item")
    public Result<PageBean<Item>> itemListWithConditionService(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) String productIdForSearch,
            @RequestParam(required = false) String state
    ) {
        System.out.println(pageNum+" "+pageSize);
        PageBean<Item> pageBean = catalogService.getItemListWithCondition(pageNum, pageSize, productIdForSearch, state);
        if (pageBean == null) {
            return Result.error("获取item条件查询失败");
        }else {
            return Result.success(pageBean);
        }
    }

    @GetMapping("/item/productList")
    public Result<List<String>> itemProductListService(){
        List<String> productList = catalogService.getItemProductList();
        if (productList == null) {
            return Result.error("获取product列表失败");
        }else {
            return Result.success(productList);
        }
    }

    @PostMapping("/product")
    public Result<String> productAddService(@Valid @RequestBody Product product, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return Result.error(bindingResult.getFieldError().getDefaultMessage());
        }
        if (catalogService.addProduct(product)) {
            return Result.success();
        }else {
            return Result.error("productid重复");
        }
    }

    @PutMapping("/product")
    public Result<String> productEditService(@Valid @RequestBody Product product, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return Result.error(bindingResult.getFieldError().getDefaultMessage());
        }
        if (catalogService.updateProduct(product)) {
            return Result.success();
        }else {
            return Result.error("更新product失败");
        }
    }

    @DeleteMapping("/product")
    public Result<String> productDeleteService(@RequestParam String productId){
        if (catalogService.deleteProduct(productId)) {
            return Result.success();
        }else {
            return Result.error("删除product失败");
        }
    }

    @GetMapping("/product")
    public Result<PageBean<Product>> productListWithConditionService(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) String categoryIdForSearch
    ){
        PageBean<Product> pageBean = catalogService.getProductListWithCondition(pageNum, pageSize, categoryIdForSearch);
        if (pageBean == null) {
            return Result.error("获取product条件查询失败");
        }else {
            return Result.success(pageBean);
        }
    }

    @GetMapping("/product/categoryList")
    public Result<List<String>> productCategoryListService(){
        List<String> categoryList = catalogService.getproductCategoryList();
        if (categoryList == null) {
            return Result.error("获取category列表失败");
        }else {
            return Result.success(categoryList);
        }
    }

    @PostMapping("/category")
    public Result<String> categoryAddService(@Valid @RequestBody Category category, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return Result.error(bindingResult.getFieldError().getDefaultMessage());
        }
        if (catalogService.addCategory(category)) {
            return Result.success();
        }else {
            return Result.error("categoryid重复");
        }
    }

    @PutMapping("/category")
    public Result<String> categoryEditService(@Valid @RequestBody Category category, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return Result.error(bindingResult.getFieldError().getDefaultMessage());
        }
        if (catalogService.updateCategory(category)) {
            return Result.success();
        }else {
            return Result.error("更新category失败");
        }
    }

    @DeleteMapping("/category")
    public Result<String> categoryDeleteService(@RequestParam String categoryId) {
        if (catalogService.deleteCategory(categoryId)) {
            return Result.success();
        }else {
            return Result.error("删除category失败");
        }
    }

    @GetMapping("/category")
    public Result<PageBean<Category>> categoryListWithConditionService(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) String categoryIdForSearch
    ){
        PageBean<Category> pageBean = catalogService.getCategoryListWithCondition(pageNum, pageSize, categoryIdForSearch);
        if (pageBean == null) {
            return Result.error("获取category条件查询失败");
        }else {
            return Result.success(pageBean);
        }
    }
}
